﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Actions;
using COFE.Core;
using COFE.Core.Lister;
using COFE.Core.Entry;

namespace COFE.Data
{
    public class CachePropertiesWork : WorkBase
    {

        private CachePropertiesWork(Guid workId, Guid entryId, string fullParsePath)
            : base(workId)
        {
            Description = "Cache_Properties_" + fullParsePath;
            _entryId = entryId;
            _entry = null;
            _primaryPropertyProvider = null;
        }

        private CachePropertiesWork(Guid workId, IPropertyProvider primaryPropertyProvider)
            : this(workId, primaryPropertyProvider.GetId(), primaryPropertyProvider.GetParseName())
        {
            _primaryPropertyProvider = primaryPropertyProvider;
        }

        private CachePropertiesWork(Guid workId, IInternalFileSystemInfoExA entry)
            : this(workId, entry.Id, entry.ParseName)
        {            
            _entry = entry;         
        }



        /// <summary>
        /// Schedule a CachePropertiesWork in COFEAction.WorkScheduler 
        /// </summary>
        /// <param name="entryId"></param>
        /// <param name="primaryPropertyProvider"> </param>
        /// <returns>Schedule id</returns>
        public static Guid ScheduleWork(IPropertyProvider primaryPropertyProvider)
        {
            var work = new CachePropertiesWork(Guid.NewGuid(), primaryPropertyProvider);
            return COFEActionShared.WorkScheduler.Enqueue(WorkPriority.Low, work);
        }

        /// <summary>
        /// Schedule a CachePropertiesWork in COFEAction.WorkScheduler.
        /// </summary>
        /// <param name="entryId"></param>
        /// <param name="entry"></param>
        /// <returns>Schedule id</returns>
        public static Guid ScheduleWork(IInternalFileSystemInfoExA entry)
        {
            var work = new CachePropertiesWork(Guid.NewGuid(), entry);
            return COFEActionShared.WorkScheduler.Enqueue(WorkPriority.Low, work);
        }

        /// <summary>
        /// Schedule a CachePropertiesWork in COFEAction.WorkScheduler.
        /// </summary>
        /// <param name="entryId">The entry must already exists in database.</param>        
        /// <returns>Schedule id</returns>
        public static Guid ScheduleWork(Guid entryId, string fullParsePath)
        {
            var work = new CachePropertiesWork(Guid.NewGuid(), entryId, fullParsePath);
            return COFEActionShared.WorkScheduler.Enqueue(WorkPriority.Low, work);
        }


        #region Methods


        protected override void DoWork()
        {
            if (_entry != null)
            {
                COFEDBShared.DBEntryPropertyManager.CacheProperties(_entryId, _entry);
            }
            else if (_primaryPropertyProvider != null)
            {
                COFEDBShared.DBEntryPropertyManager.CacheProperties(_entryId, _primaryPropertyProvider);
            }
            else
            {
                var dbEntry = COFEDBShared.DBOperations.ReadDatabase(
                    (context) => context.Entries.FirstOrDefault(e => e.EntryId == _entryId));
                if (dbEntry != null)
                {
                    string path = COFEPath.FormatFullParseName(dbEntry.PrimaryVolumeId, dbEntry.ParsePath);
                    var entry = COFEShared.CurrentUserProfile.TryParsePath(path, Core.Profile.EntryConstructionFlags.DefaultWithoutCache);
                    if (entry != null)
                        COFEDBShared.DBEntryPropertyManager.CacheProperties(_entryId, entry.InternalProperties.PrimaryPropertyProvider);
                }
            }
        }

        #endregion

        #region Data

        private Guid _entryId;
        private IInternalFileSystemInfoExA _entry;
        private IPropertyProvider _primaryPropertyProvider;

        #endregion

        #region Public Properties

        #endregion

    }
}
